Page({

  /**
   * 页面的初始数据
   */
  data: {
    xoneData: {},
    grade: 0,
    muscleAdvice: 0,
    fatAdvice: 0,
    categories: [], //bodyDate
    weightHistory: [],
    zflHistory: [],
    qztzHistory: [],
    wjyHistory: [],
    jrlHistory: [],
    dbzHistory: [],
    stsfHistory: [],
    tzlHistory: [],
    ggjHistory: [],
    nzzfHistory: [],
    jzzsHistory: [],
    jcdxHistory: [],
    znlxhHistory: [],
    bmiHistory: [],
    bodyAgeHistory: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '努力加载中...'
    })
    const xoneDataJson = wx.getStorageSync('xoneData');
    const xoneData = JSON.parse(xoneDataJson);

    const categoriesJson = wx.getStorageSync('categories');
    const categories = JSON.parse(categoriesJson);

    const weightHistoryJson = wx.getStorageSync('weightHistory');
    const weightHistory = JSON.parse(weightHistoryJson);

    const zflHistoryJson = wx.getStorageSync('zflHistory');
    const zflHistory = JSON.parse(zflHistoryJson);

    const qztzHistoryJson = wx.getStorageSync('qztzHistory');
    const qztzHistory = JSON.parse(qztzHistoryJson);

    const wjyHistoryJson = wx.getStorageSync('wjyHistory');
    const wjyHistory = JSON.parse(wjyHistoryJson);

    const jrlHistoryJson = wx.getStorageSync('jrlHistory');
    const jrlHistory = JSON.parse(jrlHistoryJson);

    const dbzHistoryJson = wx.getStorageSync('dbzHistory');
    const dbzHistory = JSON.parse(dbzHistoryJson);

    const stsfHistoryJson = wx.getStorageSync('stsfHistory');
    const stsfHistory = JSON.parse(stsfHistoryJson);

    const tzlHistoryJson = wx.getStorageSync('tzlHistory');
    const tzlHistory = JSON.parse(tzlHistoryJson);

    const ggjHistoryJson = wx.getStorageSync('ggjHistory');
    const ggjHistory = JSON.parse(ggjHistoryJson);

    const nzzfHistoryJson = wx.getStorageSync('nzzfHistory');
    const nzzfHistory = JSON.parse(nzzfHistoryJson);

    const jzzsHistoryJson = wx.getStorageSync('jzzsHistory');
    const jzzsHistory = JSON.parse(jzzsHistoryJson);

    const jcdxHistoryJson = wx.getStorageSync('jcdxHistory');
    const jcdxHistory = JSON.parse(jcdxHistoryJson);

    const znlxhHistoryJson = wx.getStorageSync('znlxhHistory');
    const znlxhHistory = JSON.parse(znlxhHistoryJson);

    const bmiHistoryJson = wx.getStorageSync('bmiHistory');
    const bmiHistory = JSON.parse(bmiHistoryJson);

    const bodyAgeHistoryJson = wx.getStorageSync('bodyAgeHistory');
    const bodyAgeHistory = JSON.parse(bodyAgeHistoryJson);

    const grade = xoneData.data.composition.figure.grade || 0;
    const muscleAdvice = parseFloat(xoneData.data.composition.muscle.value + xoneData.data.composition.muscle_control.value).toFixed(2) || 0;
    const fatAdvice = parseFloat(xoneData.data.composition.fat.value - xoneData.data.composition.fat_control.value).toFixed(2) || 0;
    this.setData({
      xoneData,
      grade,
      muscleAdvice,
      fatAdvice,
      categories,
      weightHistory,
      zflHistory,
      qztzHistory,
      wjyHistory,
      jrlHistory,
      dbzHistory,
      stsfHistory,
      tzlHistory,
      ggjHistory,
      nzzfHistory,
      jzzsHistory,
      jcdxHistory,
      znlxhHistory,
      bmiHistory,
      bodyAgeHistory
    });
    wx.hideLoading();
  },
  toDetail: function (e) {
    wx.showLoading({
      title: '请稍侯...'
    })
    const value = e.currentTarget.dataset.value;
    const unit = e.currentTarget.dataset.unit || '';
    const prv = e.currentTarget.dataset.prv || [];
    const grade = e.currentTarget.dataset.grade;
    const title = e.currentTarget.dataset.title;
    wx.setStorageSync('value', value);
    wx.setStorageSync('unit', unit);
    wx.setStorageSync('prv', JSON.stringify(prv));
    wx.setStorageSync('title', title);
    wx.setStorageSync('categories', JSON.stringify(this.data.categories));
    const that = this;
    switch (title) {
      case '体重':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.weightHistory));
        break;
      case '脂肪量':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '充足');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.zflHistory));
        break;
      case '去脂体重':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.qztzHistory));
        break;
      case '无机盐':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '不足');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.wjyHistory));
        break;
      case '肌肉量':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '不足');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.jrlHistory));
        break;
      case '蛋白质':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '不足');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.dbzHistory));
        break;
      case '身体水分':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.stsfHistory));
        break;
      case '体脂率':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.tzlHistory));
        break;
      case '骨骼肌':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '不足');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '一般');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '充足');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.ggjHistory));
        break;
      case '内脏脂肪':
        const gradeValue = parseInt(grade);
        if (gradeValue <= 1) {
          wx.setStorageSync('grade', '偏低');
        } else if (gradeValue > 1 && gradeValue <= 9) {
          wx.setStorageSync('grade', '正常');
        } else if (gradeValue > 9 && gradeValue <= 14) {
          wx.setStorageSync('grade', '偏高');
        } else if (gradeValue > 14) {
          wx.setStorageSync('grade', '过高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.nzzfHistory));
        break;
      case '健壮指数':
        wx.setStorageSync('grade', '参考');
        wx.setStorageSync('series', JSON.stringify(that.data.jzzsHistory));
        break;
      case '基础代谢':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.jcdxHistory));
        break;
      case '总能量消耗':
        wx.setStorageSync('grade', '建议');
        wx.setStorageSync('series', JSON.stringify(that.data.znlxhHistory));
        break;
      case 'B M I':
        if (grade == 1 || grade == '1') {
          wx.setStorageSync('grade', '偏低');
        } else if (grade == 2 || grade == '2') {
          wx.setStorageSync('grade', '正常');
        } else if (grade == 3 || grade == '3') {
          wx.setStorageSync('grade', '偏高');
        }
        wx.setStorageSync('series', JSON.stringify(that.data.bmiHistory));
        break;
      case '身体年龄':
        wx.setStorageSync('grade', '参考');
        wx.setStorageSync('series', JSON.stringify(that.data.bodyAgeHistory));
        break;
    }
    wx.hideLoading();
    wx.navigateTo({
      url: '/pages/xoneDataDetailHistory/xoneDataDetailHistory'
    });
  }
})